package scau.hotel.hotel_web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import scau.hotel.hotel_model.controller.RespBean;
import scau.hotel.hotel_sys.entity.SysUser;
import scau.hotel.hotel_sys.service.ISysUserService;

import javax.annotation.Resource;

/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2022-05-10 17:19:05
 */
@RestController
@RequestMapping("sysUser")
@Api(value = "typeApi", tags = "用户控制器")
@PreAuthorize("hasAuthority('admin')")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private ISysUserService sysUserService;

    /**
     * 不分页查询
     *
     * @param sysUser 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @ApiOperation("不分页查询")
    public RespBean queryAll(SysUser sysUser) {
        return RespBean.success(this.sysUserService.queryAll(sysUser));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{userId}")
    @ApiOperation("根据用户id查询数据")
    public RespBean queryById(@PathVariable("userId") Long id) {
        return RespBean.success(this.sysUserService.queryById(id));
    }

    /**
     * 通过用户名查询单条数据
     *
     * @param userName 用户名
     * @return 单条数据
     */
    @GetMapping("/queryByUserName/{userName}")
    @ApiOperation("根据用户名查询数据")
    public RespBean queryByUserName(@PathVariable("userName") String userName) {
        return RespBean.success(this.sysUserService.queryByUsername(userName));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增数据")
    public RespBean add(@RequestBody SysUser sysUser) {
        return this.sysUserService.insert(sysUser);
    }

    /**
     * 编辑数据
     *
     * @param sysUser 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑数据")
    public RespBean edit(@RequestBody SysUser sysUser) {
        return this.sysUserService.update(sysUser);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{userId}")
    @ApiOperation("根据用户id删除数据")
    public RespBean deleteById(@PathVariable("userId")Long id) {
        return this.sysUserService.deleteById(id);
    }

}

